import pickle
import load
from feature import company
import random

f=open('allcomp.pkl','rb')
allCompany=pickle.load(f)

yingxiangComp=[]

for i in range(1, 303):
    code, _ = load.getPingji(i)
    if not load.getIsCujin(i): # 抑制
        yingxiangComp.append(code)

def raodong(code): # 扰动一个公司订单
    # 随机确定百分比，减弱一些订单（买卖同时操作）
    # 随机百分比，删除一些订单（买卖同时操作）
    p1=random.uniform(0,0.9)
    p2=random.uniform(0,0.9)
    comp=allCompany[code]

    num1=min(len(comp.allXiao),len(comp.allJin)) # 要减弱的数量
    num1*=p1

    def jianruo(lis):
        for i in range(int(num1)):
            sub = random.randint(0, len(lis) - 1)
            jine, stat, day=lis[sub]
            p3=random.uniform(0,0.9)
            lis[sub]=(jine*p3,stat,day)
    jianruo(comp.allXiao)
    jianruo(comp.allJin)

    num2=min(len(comp.allXiao),len(comp.allJin))
    num2*=p2

    def shanchu(lis):
        for i in range(int(num2)):
            # 每次从当前所有元素中随机抽一个删掉
            sub=random.randint(0,len(lis)-1)
            del lis[sub]
    shanchu(comp.allXiao)
    shanchu(comp.allJin)


for code in yingxiangComp:
    raodong(code)

print('企业代号,总流水,负债率,收益率,问题订单率,周转率')
for name,comp in allCompany.items():
    comp.caluLiushui()
    comp.caluMisDingDan() # 重新计算问题订单率
    comp.sort()
    comp.getZhouzhuanLv()

    comp.output()

f = open('allCompRaodong.pkl', 'wb')
pickle.dump(allCompany, f)